
class DoubleNode{
    //存储元素
    int data;

    //存储下一个元素的地址
    DoubleNode prev;
    DoubleNode next;

    public DoubleNode(int data) {
        this.data = data;
    }

    public DoubleNode(int data, DoubleNode prev, DoubleNode next) {
        this.data = data;
        this.prev = prev;
        this.next = next;
    }
}

//无头双向链表
public class DoubleLinkedList{
    //链表长度
    private int size;
    //头节点
    private DoubleNode head;

    public int getSize() {
        return size;
    }

    public void addFirst(int data){
        if(size == 0){
            DoubleNode doubleNode = new DoubleNode(data);//根据传入的参数判断调用哪个构造方法
            head = doubleNode;
            head.prev = head;
            head.next = head;
            size++;
        }else{
            DoubleNode doubleNode = new DoubleNode(data);
            doubleNode.prev = head.prev;
            doubleNode.next = head;
            head.prev.next = doubleNode;
            head.prev = doubleNode;
            head = doubleNode;
            size++;
        }
    }
    public void addLast(int data){
        if(size == 0){
            DoubleNode doubleNode = new DoubleNode(data);
            head = doubleNode;
            head.prev = head;
            head.next = head;
            size++;
        }else {
            DoubleNode doubleNode = new DoubleNode(data);
            doubleNode.prev = head.prev;
            doubleNode.next = head;
            head.prev.next = doubleNode;
            head.prev = doubleNode;
            size++;
        }

    }
    private boolean rangeCheck(int index){
        if(index < 0 || index >= size){
            return false;
        }
        return true;
    }
    public boolean addIndex(int index,int data){
        DoubleNode doubleNode = new DoubleNode(data);
        if(index < 0 || index > size){
            return false;
        }
        if(index == 0 || head.next == head){
            addFirst(data);
        }else {
            DoubleNode node = head;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
            doubleNode.next = node;
            doubleNode.prev = node.prev;
            node.prev.next = doubleNode;
            node.prev = doubleNode;
            size++;
        }
        return true;
    }
    public boolean contains(int key){
        DoubleNode node = head;
        while (node.next != head){
            if(node.data == key){
                System.out.println("找到元素");
                return true;
            }
            node = node.next;
        }
        if(node.data == key){
            System.out.println("找到元素");
            return true;
        }
        System.out.println("没有找到该元素...");
        return false;
    }
    public void remove(int key){
        DoubleNode node = head;
        while (node.next != head){
            if(node.data == key){
                node.prev.next = node.next;
                node.next.prev = node.prev;
                size--;
                break;
            }
            node = node.next;
        }
        if(node.data == key){
            node.prev.next = node.next;
            node.next.prev = node.prev;
            size--;
        }
    }
    public void removeAllKey(int key){
        DoubleNode node = head;
        while (node.next != head){
            if(node.data == key){
                node.prev.next = node.next;
                node.next.prev = node.prev;
                size--;
            }
            node = node.next;
        }
        if(node.data == key){
            node.prev.next = node.next;
            node.next.prev = node.prev;
            size--;
        }
    }
    public void clear(){

    }

    public String toString(){
        String ret = "";
        DoubleNode doubleNode = head;
        while(doubleNode.next != head){
            ret += doubleNode.data + "->";
            doubleNode = doubleNode.next;
        }
        ret += doubleNode.data + "->";
        ret +="NULL";
        return ret;
    }


    public static void main(String[] args) {
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.addFirst(1);
        doubleLinkedList.addFirst(1);
        doubleLinkedList.addFirst(3);
        doubleLinkedList.addFirst(5);
        doubleLinkedList.addLast(2);
        doubleLinkedList.addLast(4);
        doubleLinkedList.addLast(6);
        //5->3->1->2->4->6
        System.out.println(doubleLinkedList);
        doubleLinkedList.addIndex(1,0);
        System.out.println(doubleLinkedList);
        doubleLinkedList.removeAllKey(1);
        System.out.println(doubleLinkedList);
        //5->0->3->1->2->4->6

    }


}